package com.yhp.controller;

import com.yhp.bean.BootStrapExpress;
import com.yhp.bean.Express;
import com.yhp.bean.Message;
import com.yhp.bean.ResultData;
import com.yhp.mvc.ResponseBody;
import com.yhp.service.impl.ExpressService;
import com.yhp.util.DateFormatUtil;
import com.yhp.util.GsonUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ExpressController {

    @ResponseBody("/express/console.do")
    public String console(HttpServletRequest request, HttpServletResponse response) {
        List<Map<String, Integer>> data = ExpressService.console();
        System.out.println(data);
        Message message = new Message();
        if (data.size() == 0) {
            message.setStatus(-1);
        } else {
            message.setStatus(0);
        }
        message.setData(data);
        return GsonUtil.toJSON(message);
    }

    @ResponseBody("/express/list.do")
    public String list(HttpServletRequest request, HttpServletResponse response) {
        int offset = Integer.parseInt(request.getParameter("offset"));
        int pageNumber = Integer.parseInt(request.getParameter("pageNumber"));
        List<Express> all = ExpressService.findAll(true, offset, pageNumber);
        List<BootStrapExpress> list = new ArrayList<>();
        for (Express e : all) {
            String code = e.getCode() == null ? "已经取件" : e.getCode();
            String inTime = DateFormatUtil.format(e.getInTime());
            String outTime = e.getOutTime() == null ? "" : DateFormatUtil.format(e.getOutTime());
            String status = e.getStatus() == 0 ? "未出库" : "已出库";
            BootStrapExpress bootStrapExpress = new BootStrapExpress(e.getId(), e.getNumber(), e.getUserName(), e.getUserPhone(), e.getCompany(), code, inTime, outTime, status, e.getSysPhone());
            list.add(bootStrapExpress);
        }
        List<Map<String, Integer>> console = ExpressService.console();
        ResultData<BootStrapExpress> data = new ResultData<>();
        data.setRows(list);
        data.setTotal(console.get(0).get("data1_size"));
        return GsonUtil.toJSON(data);

    }

    @ResponseBody("/express/add.do")
    public String add(HttpServletRequest request, HttpServletResponse response) {
        String number = request.getParameter("number");
        String userName = request.getParameter("userName");
        String company = request.getParameter("company");
        String phoneNumber = request.getParameter("phoneNumber");
        Express express = new Express(number, userName, phoneNumber, company, null, "111111");
        boolean insert = ExpressService.insert(express);
        System.out.println(insert);
        Message message = new Message();
        if (insert) {
            message.setStatus(0);
            message.setResult("添加成功");
        } else {
            message.setStatus(-1);
            message.setResult("添加失败");
        }
        String s = GsonUtil.toJSON(message);
        System.out.println(s);
        return s;

    }


    @ResponseBody("/express/findByNumber.do")
    public String findByNumber(HttpServletRequest request, HttpServletResponse response) {
        String number = request.getParameter("number");
        Express data = ExpressService.findByNumber(number);
        Message msg = new Message();
        if (data == null) {
            msg.setStatus(-1);
            msg.setResult("没有该快递单号");
        } else {
            msg.setStatus(0);
            msg.setData(data);
            msg.setResult("查询成功");
        }
        String s = GsonUtil.toJSON(msg);
        System.out.println(s);
        return s;
    }

    @ResponseBody("/express/update.do")
    public String update(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("wojinlaile");
        String num = request.getParameter("num");
        String number = request.getParameter("number");
        String company = request.getParameter("company");
        String username = request.getParameter("username");
        String userphone = request.getParameter("userphone");
        String status = request.getParameter("status");
        System.out.println(status);
        Express express = new Express(number, username, userphone, company, null, "111111");
        express.setStatus(Integer.parseInt(status));
        int a = Integer.parseInt(num);
        System.out.println(a);
        boolean update = ExpressService.update(a, express);
        Message msg = new Message();
        if (update) {
            msg.setStatus(0);
            msg.setResult("修改成功");
        } else {
            msg.setStatus(-1);
            msg.setResult("修改失败");
        }
        String s = GsonUtil.toJSON(msg);
        System.out.println(s);
        return s;
    }

    @ResponseBody("/express/delete.do")
    public String delete(HttpServletRequest request, HttpServletResponse response) {
        String num = request.getParameter("num");
        System.out.println(num);
        boolean delete = ExpressService.delete(Integer.parseInt(num));
        System.out.println(delete);
        Message msg = new Message();
        if (delete) {
            msg.setStatus(0);
            msg.setResult("删除成功");
        } else {
            msg.setStatus(-1);
            msg.setResult("删除失败");
        }
        String s = GsonUtil.toJSON(msg);
        return s;
    }


}
